<?php
class IntegralModel extends Model {
	
	/**
	 * 会员积分兑换记录
	 * @param $userid int 会员编号
	 * 
	 */
	public function getUserIntegralList($userid){
		
		$model = D("IntegrallogView");
		
		$map['userid'] = $userid;
		$data = $model->where($map)->select();
		
		return $data;
	}
	
	/**
	 * 会员积分兑换处理
	 */
	public function dealjifen($userid){
		$num  = I("param.num");
		$id   = I("param.id");
		$data = $this->find($id);
		$flag = array();
		
		$flag['status'] = -1;
		$mustjifen = $num*$data['integration']; //兑换商品所需积分
		
		if($num&&$id){
			//获取会员积分
			$map['userid'] = $userid;
			$json = sendRest("Vip/view",$map);
			
			if($json['data']['use_jifen']>$mustjifen){
				
				//获取积分兑换记录
				$integrallog   = M("Integrallog");
				$log['pid']    = $id;
				$log['userid'] = $userid;
				$view = $integrallog->where($log)->find();
				
				if($view['id']){
					$integrallog->where($log)->setInc("num",$num);
				}else{

					//添加兑换记录
					$list['solevar']  = create_id();
					$list['pid']      = $id;
					$list['num']      = $num;
					$list['userid']   = $userid;
					$list['addtime']  = time();
					$list['integral'] = $data['integration'];
					
					$integrallog->add($list);
				}
				$flag['status'] = 1;
				$flag['info']   = "积分商品兑换成功";
				
				//新增会员卡积分操作记录
				$map['val']    = $mustjifen;
				$map['remark'] = "积分商城兑换商品，消费积分：".$mustjifen;
				$map['total']  = 0;
				$map['cascade']= 1;
				$json = sendRest("Vip/addcredit",$map);
				
			}else{
				$flag['info'] = "积分商品兑换失败，积分不足";
			}
		}else{
			$flag['info'] = "积分商品兑换失败，请稍后重试";
		}
		return $flag;
	}
	
}
?>